﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LDSP.BRL;
using BYFW.DAL;
using System.Data;
using LDSP.Util;

namespace LDSP.UI.EnterpriseAdmin
{
    public partial class MngTaskPlan : BRL.BaseEnterpriseAdminPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialControls();
                this.InitData();
                this.GetCondition();
                this.BindGrid();
            }
        }

        protected void InitData()
        {
            //不缓存则清空页面信息
            if (!needCachePageInfo)
            {
                g_pageInfo.ClearPageInfo();
            }
            this.SetSpecialPageInfo();
        }

        /// <summary>
        /// 传入参数，设置过滤条件
        /// </summary>
        protected void SetSpecialPageInfo()
        {
            g_pageInfo.Direction = true;
            g_pageInfo.Sort = "name";
            g_pageInfo.PageSize = 10;
            g_pageInfo.PageIndex = this.grd_list.PageIndex;
        }

        private void InitialControls()
        {
            this.DropDownList1.Items.Clear();
            this.DropDownList1.Items.Add(new ListItem("所有", "0"));
            this.DropDownList1.Items.Add(new ListItem("到期未完成", "1"));
            this.DropDownList1.Items.Add(new ListItem("未到期未完成", "2"));
            this.DropDownList1.Items.Add(new ListItem("已完成", "3"));
            try
            {
                this.DropDownList1.SelectedIndex = 0;
            }
            catch { }
        }

        protected void BindGrid()
        {
            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new Plan().GetRecordsByEnterprise(this.LoginUser.EnterpriseID, g_pageInfo.ConditionWhere), g_pageInfo.GetSortExpression());

            this.grd_list.PageSize = g_pageInfo.PageSize;
            this.grd_list.PageIndex = g_pageInfo.PageIndex;
            this.grd_list.DataSource = dt;
            this.grd_list.DataBind();

            this.TotalRecords.Text = LDSP.Helper.StringUtil.GetSearchText(dt.Rows.Count);
        }

        protected void GetCondition()
        {
            string cond = "";

            if (!string.IsNullOrEmpty(this.txt_referenceNumber.Text))
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLMultiCondition("department", EDataType.String, this.txt_referenceNumber.Text, true, true), true);
            }

            if (!string.IsNullOrEmpty(this.txt_implementStartDT.Text))
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("expiredDT", EDataType.Date, this.txt_implementStartDT.Text, ">=", true), true);
            }

            if (!string.IsNullOrEmpty(this.txt_implementEndDT.Text))
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("expiredDT", EDataType.Date, this.txt_implementEndDT.Text, "<=", true), true);
            }

            if (!string.IsNullOrEmpty(this.txt_name.Text))
            {
                cond = this.AppendCondition(cond, DefaultDB.GetSQLMultiCondition("name", EDataType.String, this.txt_name.Text, true, true), true);
            }

            if (this.DropDownList1.SelectedIndex >= 1)
            {
                string status = "0";

                //已完成
                if (this.DropDownList1.SelectedValue == "3")
                {
                    status = "1";
                }

                cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("status", EDataType.Int, status, "=", true), true);

                //到期未完成
                if (this.DropDownList1.SelectedValue == "1")
                {
                    cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("expiredDT", EDataType.Date, DateTime.Now.ToShortDateString(), "<", true), true);
                }
                else if (this.DropDownList1.SelectedValue == "2")
                {//为到期未完成
                    cond = this.AppendCondition(cond, DefaultDB.GetSQLNormalCondition("expiredDT", EDataType.Date, DateTime.Now.ToShortDateString(), ">=", true), true);
                }
            }

            g_pageInfo.Condition = cond;
        }

        protected void btn_search_Click(object sender, EventArgs e)
        {
            this.GetCondition();
            this.BindGrid();
        }

        protected void grd_list_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            try
            {
                int id = this.GetInt(e.CommandArgument.ToString());
                if (e.CommandName == "deleteItem")
                {
                    Plan ett = new Plan();
                    ett.GetProperties(id);
                    ett.Delete();
                }
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Plan ett = new Plan();
                    ett.GetProperties(((DataRowView)e.Row.DataItem).Row);
                    this.ShowSequenceNumber(e.Row, 1);
                    e.Row.Cells[1].Text = ett.EnterpriseCodification.Codification.Name;
                    e.Row.Cells[5].Text = this.FormatDT(ett.ExpiredDT);
                    e.Row.Cells[7].Text = this.FormatDT(ett.ChangeDT);
                    e.Row.Cells[9].Text = ett.Status == 1 ? "是" : "否";

                    //响应搜索条件，法律法规标准名称
                    if (!ett.EnterpriseCodification.Codification.Name.Contains(this.txt_cod_name.Text))
                    {
                        e.Row.Visible = false;
                    }
                }
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                g_pageInfo.PageIndex = e.NewPageIndex;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }
        protected void grd_list_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                if (g_pageInfo.Sort == e.SortExpression)
                {
                    g_pageInfo.Direction = !g_pageInfo.Direction;
                }
                else
                {
                    g_pageInfo.Direction = true;
                }
                g_pageInfo.Sort = e.SortExpression;
                this.BindGrid();
            }
            catch (Exception ex)
            {
                this.HandlerException(ex);
            }
        }

        private void JSShowModalDialog(string arg, WebControl ctrl)
        {
            this.JSShowDialog("AddTaskPlan.aspx?" + arg, 800, 400, ctrl, null, null, null, null, null, null, null, true, null, null, null, null, null);
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            this.GetCondition();
            DataTable dt = BYFW.SFL.CommonOP.SortedTable(new Plan().GetRecordsByEnterprise(this.LoginUser.EnterpriseID, g_pageInfo.ConditionWhere), g_pageInfo.GetSortExpression());

            TaskPlanExport.Export(this.Server, dt);
        }
    }
}